package com.boxin.ims.modules.wechat.utils;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Service;

import com.boxin.framework.base.utils.DateUtils;
import com.boxin.ims.modules.momarketing.entity.Coupon;
import com.boxin.ims.modules.momarketing.entity.CouponConfig;
import com.boxin.ims.modules.momarketing.service.CouponConfigService;
import com.boxin.ims.modules.wechat.entity.MemberScore;
import com.boxin.ims.modules.wechat.entity.WechatMember;
import com.boxin.ims.modules.wechat.score.entity.ExchangeRecord;
import com.boxin.ims.modules.wechat.score.entity.ScoreExchange;
import com.boxin.ims.modules.wechat.score.service.ExchangeRecordService;
import com.boxin.ims.modules.wechat.service.MemberScoreService;
import com.boxin.ims.modules.wechat.service.WechatMemberService;
import com.thinkgem.jeesite.modules.sys.entity.User;

@Service
public class ScoreUtils   implements ApplicationContextAware {
	
	public static Integer DEFAULT_ADD_SCORE = 1;
	
	private static MemberScoreService memberScoreService;
	
	private static WechatMemberService wechatMemberService;
	
	private static CouponConfigService couponConfigService;
	
	private static ExchangeRecordService exchangeRecordService;
	
	/**
	 * @author Jakemanse
	 * @time 2013-9-9  上午11:49:38
	 * @function <p>微信签到积分 </p>
	 * @param wechatMember
	 */
	public static void addWechatMemberSignScore(WechatMember wechatMember,int score ){
		
		MemberScore memberScore  = new MemberScore();
		memberScore.setWechatMember(wechatMember);
		memberScore.setUser(wechatMember.getUser());
		memberScore.setScore(score);			//操作的分数，有可能是负数 
		memberScore.setRemarks("微信签到积分");
		memberScoreService.save(memberScore);
	}
	
	
	/**
	 * @author Jakemanse
	 * @time 2013-10-24  下午3:08:33
	 * @function <p> 添加或扣除积分 </p>
	 * @param wechatMember
	 * @param score
	 * @param reason
	 */
	public static void addScore(WechatMember wechatMember,int score,String reason ){
		
		int resultScore = wechatMember.getScore() + score;
		wechatMember.setScore((resultScore<0)?0:resultScore);
		wechatMemberService.save(wechatMember);

		MemberScore memberScore  = new MemberScore();
		memberScore.setWechatMember(wechatMember);
		memberScore.setUser(wechatMember.getUser());
		memberScore.setScore(score);			//操作的分数，有可能是负数 
		memberScore.setRemarks(reason);
		memberScoreService.save(memberScore);
	}
	
	/**
	 * @author Jakemanse
	 * @time 2013-9-25  上午8:57:10
	 * @function <p>会员充值  </p>
	 * @param wechatMember
	 * @param score
	 */
	public static void addWechatMemberChargeScore(WechatMember wechatMember,int score ){
		
		MemberScore memberScore  = new MemberScore();
		memberScore.setWechatMember(wechatMember);
		memberScore.setUser(wechatMember.getUser());
		memberScore.setScore(score);			//操作的分数，有可能是负数 
		memberScore.setRemarks("微信会员积分充值");
		memberScoreService.save(memberScore);
	}
	
	
	
	
	
	/**
	 * @author Jakemanse
	 * @time 2013-9-25  上午8:57:10
	 * @function <p>取消关注，积分清零  </p>
	 * @param wechatMember
	 * @param score
	 */
	public static void addWechatCancelScore(WechatMember wechatMember,int score ){
		
		MemberScore memberScore  = new MemberScore();
		memberScore.setWechatMember(wechatMember);
		memberScore.setUser(wechatMember.getUser());
		memberScore.setScore(-score);			//操作的分数，有可能是负数 
		memberScore.setRemarks("取消关注后，扣除"+score+"积分清零.");
		memberScoreService.save(memberScore);
	}
	
	
	/**
	 * @author Jakemanse
	 * @time 2014-1-20  上午12:25:47
	 * @function <p> 积分兑换 </p>
	 * @param wechatMember
	 * @param score
	 * @param remarks
	 */
	public static void addWechatExchangeScore(WechatMember wechatMember,int score,String remarks ){
		
		MemberScore memberScore  = new MemberScore();
		memberScore.setWechatMember(wechatMember);
		memberScore.setUser(wechatMember.getUser());
		memberScore.setScore(-score);			//操作的分数，有可能是负数 
		memberScore.setRemarks(remarks);
		memberScoreService.save(memberScore);
	}
	
	
	
	
	/**
	 * @author Jakemanse
	 * @time 2013-9-24  下午2:19:25
	 * @function <p> 积分兑换，消费 </p>
	 * @param wechatMember
	 * @param score
	 */
	public static ExchangeRecord  addWechatMemberConsumeScore(WechatMember wechatMember, ScoreExchange exchange ){
		
		MemberScore memberScore  = new MemberScore();
		memberScore.setWechatMember(wechatMember);
		memberScore.setUser(wechatMember.getUser());
		memberScore.setScore(-exchange.getScore());			//操作的分数， 是负数 
		memberScore.setRemarks("微信积分兑换" + exchange.getName() +",消费了"+exchange.getScore() +"积分.");
		memberScoreService.save(memberScore);
		//添加积分兑换记录
		ExchangeRecord record = new ExchangeRecord();
		record.setCostScore(exchange.getScore());
		record.setExchange(exchange);
		record.setUser(wechatMember.getUser());
		record.setMember(wechatMember);
		record.setName(exchange.getName());
		record.setUser(wechatMember.getUser());
		record.setEndTime(DateUtils.getDelayDate(exchange.getValidDays()));
		record.setStatus("0");
		exchangeRecordService.save(record);
		  
		return record;
	}
	
	
	/**
	 * @author Jakemanse
	 * @time 2013-9-30  下午1:13:43
	 * @function <p> 积分扣除   </p>
	 * @param wechatMember
	 * @param exchange
	 */
	public static void addWechatMemberCostScore(WechatMember wechatMember, int cost, String remarks){
		
		MemberScore memberScore  = new MemberScore();
		memberScore.setWechatMember(wechatMember);
		memberScore.setUser(wechatMember.getUser());
		memberScore.setScore(-cost);			//操作的分数， 是负数 
		memberScore.setRemarks(remarks);
		
		memberScoreService.save(memberScore);
	}
	
	 
	
	
	/**
	 * @author Jakemanse
	 * @time 2013-9-9  上午11:53:17
	 * @function <p> 优惠券核销积分 </p>
	 * @param wechatMember
	 */
	public static void addWechatMemberCouponScore(WechatMember wechatMember,Coupon coupon,int addScore){
		
		MemberScore memberScore  = new MemberScore();
		memberScore.setWechatMember(wechatMember);
		memberScore.setScore(addScore);
		memberScore.setRemarks("优惠券核销积分,券码为:"+ coupon.getNumber());
		memberScoreService.save(memberScore);
		
	}


	@Override
	public void setApplicationContext(ApplicationContext applicationContext)
			throws BeansException {
		
		memberScoreService = (MemberScoreService)applicationContext.getBean("memberScoreService");
		couponConfigService = (CouponConfigService)applicationContext.getBean("couponConfigService");
		exchangeRecordService = (ExchangeRecordService)applicationContext.getBean("exchangeRecordService");
		wechatMemberService = (WechatMemberService)applicationContext.getBean("wechatMemberService");
		
	}
	
	
	
	
	

}
